package date;

import java.time.*;
import java.time.temporal.ChronoUnit;

public class TestLocalDateTime05 {
    public static void main(String[] args) {

    }
    public static void test01(){
        /**
         * 要判断两个LocalDateTime的先后，可以使用isBefore()、isAfter()方法
         */
        //判断两个时间点的前后
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime target = LocalDateTime.of(2022, 2, 15, 16, 15, 0);
        System.out.println(now.isBefore(target));
        System.out.println(LocalDate.now().isBefore(LocalDate.of(2022, 2, 19)));
        System.out.println(LocalTime.now().isAfter(LocalTime.parse("08:15:00")));
    }

    public static void test02(){
        /**计算两个日期的日期间隔-年月日
         * Period:用于计算两个“日期”间隔
         */
        LocalDate date1 = LocalDate.of(2020, 10, 21);
        LocalDate date2 = LocalDate.of(2019, 9, 11);
        //内部是用date2-date1，所以得到的结果是负数
        Period period = Period.between(date1, date2);
        System.out.println("相差年数 ： " + period.getYears());
        System.out.println("相差月数 ： " + period.getMonths());
        System.out.println("相差日数 ： " + period.getDays());
        //另一种计算方式和表现形式
        System.out.println("-------------------------------");
        long years = period.get(ChronoUnit.YEARS);
        long months = period.get(ChronoUnit.MONTHS);
        long days = period.get(ChronoUnit.DAYS);
        System.out.println("相差的年月日分别为 ： " + years + "," + months + "," + days);
    }

    public static void test03(){
        /**计算两个时间的间隔
         * Duration:用于计算两个“时间”间隔
         */
        System.out.println("-------------------------------");
        LocalDateTime date3 = LocalDateTime.now();
        LocalDateTime date4 = LocalDateTime.of(2018, 1, 13, 22, 30, 10);
        Duration duration = Duration.between(date3, date4);
        System.out.println(date3 + " 与 " + date4 + " 间隔  " + "\n"
                + " 天 :" + duration.toDays() + "\n"
                + " 时 :" + duration.toHours() + "\n"
                + " 分 :" + duration.toMinutes() + "\n"
                + " 毫秒 :" + duration.toMillis() + "\n"
                + " 纳秒 :" + duration.toNanos() + "\n"
        );
    }
}
